package com.tankgame.utils;

import java.util.Vector;
import com.tankgame.models.BaseTank;

public class UniqueTankVector extends Vector<BaseTank> {

  @Override
  public synchronized boolean add(BaseTank tank) {
    for (int i = 0; i < this.size(); i++) {
      if (this.get(i).getId() == tank.getId()) {
        this.set(i, tank);
        return true;
      }
    }
    return super.add(tank);
  }

  @Override
  public synchronized void addElement(BaseTank tank) {
    for (int i = 0; i < this.size(); i++) {
      if (this.get(i).getId() == tank.getId()) {
        this.set(i, tank);
        return;
      }
    }
    super.addElement(tank);
  }

  @Override
  public synchronized void add(int index, BaseTank tank) {
    for (int i = 0; i < this.size(); i++) {
      if (this.get(i).getId() == tank.getId()) {
        this.set(i, tank);
        return;
      }
    }
    super.add(index, tank);
  }
}